package com.test.travelinformation.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.test.travelinformation.R;
import com.test.travelinformation.adapter.CircleAdapter;
import com.test.travelinformation.dao.TravelDao;
import com.test.travelinformation.entity.Collect;
import com.test.travelinformation.entity.Travel;
import com.test.travelinformation.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class ReleaseActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    CircleAdapter adapte;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release);
        recyclerView = findViewById(R.id.recyclerView);
        initview();
    }

    private void initview() {
        adapte = new CircleAdapter(this, new ArrayList<>());
        TravelDao travelDao = new TravelDao();
        travelDao.getIssueData(adapte, SPUtils.get(this, "userId", "").toString());
        adapte.setOnItemLongClickListener((adapter, view, position) -> {
            new QMUIDialog.MessageDialogBuilder(this)
                    .setTitle("删除信息")
                    .setMessage("确认要删除这条信息吗？")
                    .addAction("取消", (dialog, index) -> dialog.dismiss())
                    .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, (dialog, index) -> {
                        Travel travel = ((Travel)adapter.getData().get(position));
                        travel.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null){
                                    Toast.makeText(ReleaseActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                                    adapte.setNewInstance(new ArrayList<>());
                                    TravelDao travelDao = new TravelDao();
                                    travelDao.getIssueData(adapte, SPUtils.get(ReleaseActivity.this, "userId", "").toString());
                                }else {
                                    Toast.makeText(ReleaseActivity.this,"删除失败"+e.toString(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        dialog.dismiss();
                    }).create().show();
            return true;
        });

        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.setAdapter(adapte);
    }
}
